/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. 3 as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.*;

class StaticDoubleTranslator extends PrimitiveBaseTranslator implements ITranslator {
   public String name() {
      return "double";
   }

   public String php_name() {
      return "float";
   }
}

/**
 * @composed 1 - 1 DoubleValueTranslator
 */
class DoubleTranslator extends StaticDoubleTranslator implements ITypeTranslator {
   private static IValueTranslator value_translator = new DoubleValueTranslator();

   static {
      JavaType.add_type_translator( new DoubleTranslator() );
   }

   private DoubleTranslator() {}

   public IValueTranslator value_translator() {
      return value_translator;
   }

}

class DoubleValueTranslator extends StaticDoubleTranslator implements IValueTranslator {

   static Translation literal(double value) {
      return new Translation(""+value, JavaType._double(), true, true);
   }

   public Translation literal(String value) throws TranslatorException {
      return literal( Double.parseDouble( value.replaceAll("f|F|d|D", "") ) );
   }

   public Translation add(Translation left, Translation right) throws TranslatorException {
      return new Translation(left.text +" + "+ right.text, JavaType._double(), false, left.not_null && right.not_null);
   }

   public Translation add_eq(Translation left, Translation right) throws TranslatorException {
      String left_text = left.text;
      if( right.modifiedVariables.contains(left_text) ) {
         return new Translation(left_text + " = " + left_text +" + "+ right.text, JavaType._double(), false, left.not_null && right.not_null, right.modifiedVariables);
      }

      Set<String> modifiedVariables = new HashSet<String>( right.modifiedVariables );
      modifiedVariables.add(left_text);
      return new Translation(left_text + " += " + right.text, JavaType._double(), false, left.not_null && right.not_null, modifiedVariables);
   }

   public Translation div(Translation left, Translation right) throws TranslatorException {
      return new Translation(left.text +" / "+ right.text, JavaType._double(), false, left.not_null && right.not_null);
   }

   public Translation div_eq(Translation left, Translation right) throws TranslatorException {
      String left_text = left.text;
      if( right.modifiedVariables.contains(left_text) ) {
         return new Translation(left_text + " = " + left_text +" / "+ right.text, JavaType._double(), false, left.not_null && right.not_null, right.modifiedVariables);
      }

      Set<String> modifiedVariables = new HashSet<String>( right.modifiedVariables );
      modifiedVariables.add(left_text);
      return new Translation(left_text + " /= " + right.text, JavaType._double(), false, left.not_null && right.not_null, modifiedVariables);
   }
}
